package com.itheima.tlias.service.impl;

import com.itheima.tlias.model.entity.Dept;
import com.itheima.tlias.mapper.DeptMapper;
import com.itheima.tlias.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author byzhao
 * @created 2024-04-05 16:56:02
 * @describtion ① 业务实现类 实现 业务接口
 * ② @Service 注解将DeptServiceImpl 放入 IOC 容器
 */
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    DeptMapper deptMapper;

    @Override
    public void updata(Dept dept) {
        if(ObjectUtils.isEmpty(dept.getName())
        ||ObjectUtils.isEmpty(dept.getId())){
            return;
        }

        deptMapper.updata(dept);
    }

    @Override
    public Dept deptById(Integer id) {
       return deptMapper.deptById(id);
    }

    @Override
    public void save(Dept dept) {

        // 1. 参数处理
        if (ObjectUtils.isEmpty(dept.getName())){
            return;
        }
        // 2.  业务处理
        // 2.1 补全数据 创建时间和修改时间
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insertDept(dept);

    }

    @Override
    public void delById(int id) {
        deptMapper.deleteById(id);
    }

    @Override
    public List<Dept> getDepts() {

        return deptMapper.selectDepts();
    }
}
